<script type='text/javascript'>
    $(function(){
        var totalPageValue = <#if page?? && page.totalPages??>${page.totalPages!}<#else>0</#if>;
        var pageNoValue = <#if page?? && page.pageNo??>${page.pageNo!}<#else>1</#if>;
        refreshPages(totalPageValue,pageNoValue);
    });

    function refreshPages(totalPage, currentPage) {
        //安全判断
        if (currentPage < 1 ) {
            currentPage = 1;
        }else if (currentPage > totalPage) {
            currentPage = totalPage;
        }
        var paginationInfo = getPagination(totalPage, currentPage);
        //用id选择器写入页码部分代码（根据需求修改）
        $("#ccPagenitor").html(paginationInfo);
    }

    function searchJob(page){
        document.getElementById("pageNo").value=page;
        document.getElementById("pageForm").submit();
    }
    function getPagination(totalPage, currentPage){

        var paginationInfo = "<ul class='pagination' style='flex-grow: 1'>";
        if (currentPage == 1) {
            paginationInfo += "<li class='page-item disabled'><a class='page-link' href='javascript:void(0);' onclick='searchJob(" + (currentPage - 1) + ")'"+">«</a></li>";
        }else {
            //前一页
            paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(" + (currentPage - 1) + ")'"+">«</a></li>";
        }

        if(totalPage <= 10){
            //totalPage<=10
            for(var i = 1; i <= totalPage; i++){
                if (i == currentPage) {
                    paginationInfo += "<li class='page-item active'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>" + i + " </a></li>";
                }else {
                    paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>" + i + " </a></li>";
                }
            }
        } else{
            //totalPage > 10
            if(currentPage <= 3){
                for(var i = 1; i <= currentPage + 2; i++){
                    //页码1、2
                    if (i == currentPage) {
                        paginationInfo += "<li class='page-item active'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>" + i + "</a></li>";
                    } else {
                        paginationInfo += "<li class='page-item'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>" + i + "</a></li>";
                    }
                }
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);'>...</a></li>";
                //最后一页的页码
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(" + totalPage + ")'>" + totalPage + "</a></li>";
            }else if(currentPage <= totalPage - 5){
                //totalPage > 10   currentPage > 3 currentPage<=totalPage-5，  页码在中间部分
                //页码为1的代码
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(1)'>" + 1 + "</a></li>";

                //页码1后面的省略号
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);'>...</a></li>";

                //中间部分代码
                for(var i = currentPage - 1; i <= currentPage + 2; i++){
                    if (i == currentPage) {
                        paginationInfo += "<li class='page-item active'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>" + i + "</a></li>";
                    } else {
                        paginationInfo += "<li class='page-item'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>" + i + "</a></li>";
                    }
                }
                //后面的省略号
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);'>...</a></li>";
                //最后一个页码
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(" + totalPage + ")'>" + totalPage + "</a></li>";
            }else{
                //totalPage > 10  并且currentPage > totalPage-5 显示后面的页码

                //页码1
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(1)'>"+1+"</a></li>";
                //省略号
                paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);'>...</a></li>";
                //最后几位页码
                for(var i = currentPage - 1; i <= totalPage; i++){
                    if (i == currentPage) {
                        paginationInfo += "<li class='page-item active'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + "'>" + i + "</a></li>";
                    }else {
                        paginationInfo += "<li class='page-item'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + i + ")'>"+i+"</a></li>";
                    }
                }
            }
        }

        //下一页
        if (currentPage == totalPage) {
            paginationInfo += "<li class='page-item disabled'> <a class='page-link' href='javascript:void(0);' onclick='searchJob(" + (currentPage + 1) + ")'" + ">»</a></li>";
        } else {
            paginationInfo += "<li class='page-item'><a class='page-link' href='javascript:void(0);' onclick='searchJob(" + (currentPage + 1) + ")'"+">»</a></li>";
        }
        paginationInfo += "</ul>";
        //返回结果
        return paginationInfo;
    }
</script>